package com.demo.jetpack

import android.app.Application
import android.os.Bundle
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.demo.jetpack.databinding.MainLayoutBinding

class MainActivity: AppCompatActivity() {

    private val _mSharedViewModel by lazy {
        ViewModelProvider(this, defaultViewModelProviderFactory)[SharedViewModel::class.java]
    }

    private val mViewBinding by lazy {
        MainLayoutBinding.inflate(layoutInflater)
    }

    private val mSharedViewModel by viewModels<SharedViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(mViewBinding.root)
        DataBindingUtil.bind<MainLayoutBinding>(mViewBinding.root)?.apply {
            user = mSharedViewModel.user.value
        }
    }

}

class SharedViewModel(val user: MutableLiveData<User> = MutableLiveData(User("李建伟", 10))): ViewModel()

class SharedAndroidViewModel(application: Application): AndroidViewModel(application)